﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using AssemblyViewer.Common;

namespace AssemblyViewer.Model
{
    class AssemblyNameInfo
    {
        public string AssemblyName { get; private set; }

        public string Version { get; private set; }

        public string Culture { get; private set; }

        public string PublicKeyToken { get; private set; }

        public String BuildMode { get; private set; }

        public string ProcessorArchitecture { get; private set; }

        public string FullName { get; private set; }

        public string Remark { get; set; }

        public AssemblyNameInfo(AssemblyName anItem, string assemblyFilePath)
        {
            AssemblyName = anItem.Name;
            Version = anItem.Version.ToString();
            //懒惰匹配
            Culture = Regex.Match(anItem.FullName, @"Culture=(.*?),").Groups[1].Value;
            //贪婪匹配
            PublicKeyToken = Regex.Match(anItem.FullName, @"PublicKeyToken=(.*)").Groups[1].Value;
            FullName = anItem.FullName;

            var filePath = Path.Combine(assemblyFilePath, anItem.Name + ".dll");
            Remark = AssemblyHelper.ValidateAssembly(anItem.FullName, filePath);
            if (!string.IsNullOrEmpty(Remark))
            {
                return;
            }

            var assembly = AssemblyHelper.Load(anItem.FullName, filePath);
            if (assembly != null)
            {
                ProcessorArchitecture = assembly.GetProcessorArchitecture();
                BuildMode = assembly.GetBuildMode(); 
            }
        }
    }
}
